package com.xingzhi.shortvideosharingplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingzhi.shortvideosharingplatform.mapper.SystemNotificationMapper;
import com.xingzhi.shortvideosharingplatform.model.SystemNotification;
import com.xingzhi.shortvideosharingplatform.model.Video;
import com.xingzhi.shortvideosharingplatform.service.SystemNotificationService;
import com.xingzhi.shortvideosharingplatform.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class SystemNotificationServiceImpl extends ServiceImpl<SystemNotificationMapper, SystemNotification> implements SystemNotificationService {

    @Autowired
    private VideoService videoService;

    @Override
    public boolean sendLikeNotification(Long userId, Long likeUserId, Long videoId) {
        try {
            SystemNotification notification = new SystemNotification();
            notification.setUserId(userId);
            notification.setType(1L); // 1表示点赞通知
            notification.setSourceId(videoId); // 来源ID为视频ID
            notification.setIsRead(0L); // 0表示未读
            
            // 获取视频标题，作为通知内容的一部分
            Video video = videoService.getById(videoId);
            String videoTitle = video != null ? video.getTitle() : "您的视频";
            
            // 通知内容：用户xxx点赞了你的视频《xxx》
            String content = "用户ID为 " + likeUserId + " 点赞了你的视频《" + videoTitle + "》";
            notification.setContent(content);
            
            notification.setCreatedTime(LocalDateTime.now());
            
            return save(notification);
        } catch (Exception e) {
            log.error("发送点赞通知失败: userId={}, likeUserId={}, videoId={}", userId, likeUserId, videoId, e);
            return false;
        }
    }

    @Override
    public boolean sendFavoriteNotification(Long userId, Long favoriteUserId, Long videoId) {
        try {
            SystemNotification notification = new SystemNotification();
            notification.setUserId(userId);
            notification.setType(6L); // 6表示收藏通知
            notification.setSourceId(videoId); // 来源ID为视频ID
            notification.setIsRead(0L); // 0表示未读
            
            // 获取视频标题，作为通知内容的一部分
            Video video = videoService.getById(videoId);
            String videoTitle = video != null ? video.getTitle() : "您的视频";
            
            // 通知内容：用户xxx收藏了你的视频《xxx》
            String content = "用户ID为 " + favoriteUserId + " 收藏了你的视频《" + videoTitle + "》";
            notification.setContent(content);
            
            notification.setCreatedTime(LocalDateTime.now());
            
            return save(notification);
        } catch (Exception e) {
            log.error("发送收藏通知失败: userId={}, favoriteUserId={}, videoId={}", userId, favoriteUserId, videoId, e);
            return false;
        }
    }

    @Override
    public boolean sendFollowNotification(Long userId, Long followUserId) {
        try {
            SystemNotification notification = new SystemNotification();
            notification.setUserId(userId);
            notification.setType(3L); // 3表示关注通知
            notification.setSourceId(followUserId); // 来源ID为关注者ID
            notification.setIsRead(0L); // 0表示未读
            
            // 通知内容：用户xxx关注了你
            String content = "用户ID为 " + followUserId + " 关注了你";
            notification.setContent(content);
            
            notification.setCreatedTime(LocalDateTime.now());
            
            return save(notification);
        } catch (Exception e) {
            log.error("发送关注通知失败: userId={}, followUserId={}", userId, followUserId, e);
            return false;
        }
    }

    @Override
    public boolean markAsRead(Long notificationId, Long userId) {
        try {
            LambdaUpdateWrapper<SystemNotification> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SystemNotification::getId, notificationId)
                        .eq(SystemNotification::getUserId, userId) // 确保只能标记自己的通知
                        .set(SystemNotification::getIsRead, 1L);
            return update(updateWrapper);
        } catch (Exception e) {
            log.error("标记通知已读失败: notificationId={}, userId={}", notificationId, userId, e);
            return false;
        }
    }

    @Override
    public int getUnreadCount(Long userId) {
        try {
            LambdaQueryWrapper<SystemNotification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SystemNotification::getUserId, userId)
                      .eq(SystemNotification::getIsRead, 0L);
            return (int) count(queryWrapper);
        } catch (Exception e) {
            log.error("获取未读通知数量失败: userId={}", userId, e);
            return 0;
        }
    }

    @Override
    public void sendCommentNotification(Long receiverId, Long senderId, Long videoId, Long commentId, String content) {
        try {
            SystemNotification notification = new SystemNotification();
            notification.setUserId(receiverId);  // 接收通知的用户ID
            notification.setType(2L);  // 2表示评论通知
            notification.setSourceId(commentId);  // 评论ID作为来源
            notification.setContent(String.format("用户 %d 评论了你的视频：%s", senderId, content));
            notification.setIsRead(0L);  // 0表示未读
            notification.setCreatedTime(LocalDateTime.now());
            
            save(notification);
            log.info("发送评论通知成功: receiverId={}, senderId={}, videoId={}, commentId={}", 
                    receiverId, senderId, videoId, commentId);
        } catch (Exception e) {
            log.error("发送评论通知失败", e);
        }
    }

    @Override
    public void sendReplyNotification(Long receiverId, Long senderId, Long videoId, Long commentId, String content) {
        try {
            SystemNotification notification = new SystemNotification();
            notification.setUserId(receiverId);  // 接收通知的用户ID
            notification.setType(7L);  // 7表示回复通知
            notification.setSourceId(commentId);  // 评论ID作为来源
            notification.setContent(String.format("用户 %d 回复了你的评论：%s", senderId, content));
            notification.setIsRead(0L);  // 0表示未读
            notification.setCreatedTime(LocalDateTime.now());
            
            save(notification);
            log.info("发送回复通知成功: receiverId={}, senderId={}, videoId={}, commentId={}", 
                    receiverId, senderId, videoId, commentId);
        } catch (Exception e) {
            log.error("发送回复通知失败", e);
        }
    }

    @Override
    public void sendMentionNotification(Long receiverId, Long senderId, Long videoId, Long commentId, String content) {
        try {
            SystemNotification notification = new SystemNotification();
            notification.setUserId(receiverId);  // 接收通知的用户ID
            notification.setType(4L);  // 4表示@通知
            notification.setSourceId(commentId);  // 评论ID作为来源
            notification.setContent(String.format("用户 %d 在评论中提到了你：%s", senderId, content));
            notification.setIsRead(0L);  // 0表示未读
            notification.setCreatedTime(LocalDateTime.now());
            
            save(notification);
            log.info("发送@通知成功: receiverId={}, senderId={}, videoId={}, commentId={}", 
                    receiverId, senderId, videoId, commentId);
        } catch (Exception e) {
            log.error("发送@通知失败", e);
        }
    }
    
    @Override
    public void sendCommentLikedNotification(Long receiverId, Long senderId, Long videoId, Long commentId, String content) {
        try {
            SystemNotification notification = new SystemNotification();
            notification.setUserId(receiverId);  // 接收通知的用户ID（评论所有者）
            notification.setType(8L);  // 8表示评论被点赞通知
            notification.setSourceId(commentId);  // 评论ID作为来源
            notification.setContent(String.format("用户 %d 点赞了你的评论：%s", senderId, content));
            notification.setIsRead(0L);  // 0表示未读
            notification.setCreatedTime(LocalDateTime.now());
            
            save(notification);
            log.info("发送评论点赞通知成功: receiverId={}, senderId={}, videoId={}, commentId={}", 
                    receiverId, senderId, videoId, commentId);
        } catch (Exception e) {
            log.error("发送评论点赞通知失败", e);
        }
    }
} 